package tag36_29_08.regExWieder;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.nio.charset.MalformedInputException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AufgabePhilosophen {

	// ------------------------------------------------------------------------------
	static String loadFromUrl() throws MalformedInputException, IOException {
		StringBuilder sb = new StringBuilder();

		URL url = new URL(
				"http://de.wikipedia.org/wiki/Liste_antiker_Philosophen");

		InputStream is = url.openStream();

		InputStreamReader reader = new InputStreamReader(is, "UTF-8");

		BufferedReader in = new BufferedReader(reader);

		try {
			String line;
			while ((line = in.readLine()) != null) {
				sb.append(line);
			}
		} finally {
			in.close();
		}

		return sb.toString();
	}

	// ------------------------------------------------------------------------------
	static void saveToFile(String text, String fileName)
			throws FileNotFoundException {
		PrintWriter out = new PrintWriter(fileName);
		try {
			out.print(text);
		} finally {
			out.close();
		}
	}

	// ------------------------------------------------------------------------------
	static String loadFromFile(String fileName) throws IOException {
		BufferedReader in = new BufferedReader(new FileReader(fileName));

		StringBuilder sb = new StringBuilder();
		try {
			String line;
			while ((line = in.readLine()) != null) {
				sb.append(line);
			}
		} finally {
			in.close();
		}

		return sb.toString();
	}

	// ------------------------------------------------------------------------------
	// rausgeparste Strings:
	// <li><a href="/wiki/Apuleius" title="Apuleius">Apuleius</a></li>
	// <li><b><a href="/wiki/Heraklit" title="Heraklit">Heraklit</a></b> (535
	// und 540 v. Chr.), Vorsokratiker, erster Einsiedler Europas</li>
	// ...
	// Die letzten 7 sind falsch (werden entfernt):
	// <li><a href="/wiki/%C3%96stliche_Philosophie"
	// title="�stliche Philosophie">�stliche Philosophie</a></li>
	// ...
	static List<String> getRows(String text) {
		List<String> list = new ArrayList<String>();

		Pattern p = Pattern.compile("<li><.*?</li>");
		Matcher m = p.matcher(text);

		while (m.find()) {
			list.add(m.group());
			System.out.println(m.group());
		}

		list = list.subList(0, list.size() - 7); // Die 7 letzten falschen
													// Eintr�ge l�schen

		return list;
	}

	static String getName(String row) {
		Pattern p = Pattern.compile("\">.+?</a");
		Matcher m = p.matcher(row);
		m.find();
		return m.group().replace("\">", "").replace("</a", "");
	}

	// ------------------------------------------------------------------------------
	static List<String> parseNamesFromHtml(String text) {
		List<String> names = new ArrayList<String>();

		List<String> rows = getRows(text);
		for (String rw : rows) {
			String name = getName(rw);
			names.add(name);
		}

		return names;
	}

	// ------------------------------------------------------------------------------
	public static void main(String[] args) throws Exception {

		String text;
		String fileName = "philosophen.html";

		// text = loadFromUrl();
		// saveToFile(text, fileName);

		text = loadFromFile(fileName);
		// System.out.println(text);

		List<String> listNamen = parseNamesFromHtml(text);
		for (String name : listNamen) {
			System.out.println(name);
		}

		System.out.println("------------------------------------");
		System.out.println("Namen gefunden: " + listNamen.size());
		

	} // end of main
	
}
